//
//  ViewController.m
//  MultiTouch
//
//  Created by iD Student on 7/9/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoadWithBackgroundColor:(UIColor *)color
{
    [super viewDidLoad];
    image = [[UIImage alloc]init];
    image = [UIImage imageNamed:@"ball.png"];
    image2 = [[UIImage alloc]init];
    image2 = [UIImage imageNamed:@"ball2.png"];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(update) userInfo:nil repeats:YES];
    particles = [[NSMutableArray alloc]init];
    previousPoint = CGPointMake(160, 240);
    imageRocket = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rocket.png"]];
	// Do any additional setup after loading the view, typically from a nib.
    if(color == nil)
    {
        
    } else {
        self.view.backgroundColor = color;
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouching = true;
    touch = [touches anyObject];
    point = [touch locationInView:self.view];
    [NSTimer timerWithTimeInterval:.033 target:self selector:@selector(checkIfTouchMoved) userInfo:nil repeats:YES];
}

- (BOOL) checkIfTouchMoved
{
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouching = false;
    touch = [touches anyObject];
    point = [touch locationInView:self.view];
    previousPoint = [touch previousLocationInView:self.view];
    
    int particlesToSpawn = 5;
    int maxLifeTime = 10;
    for (int i = 0; i < particlesToSpawn; i++) {
        Particle *particle = [[Particle alloc]init:image];
        particle.image.center = point;
        particle.velocityX = (previousPoint.x - point.x);
        particle.velocityY = (previousPoint.y - point.y);
        particle.lifetime = arc4random() % maxLifeTime + 30;
        [particles addObject:particle];
        [self.view addSubview:particle.image];
    }
    
    
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouching = false;
}

- (void) update
{
    int i = 0;
    while (i < particles.count) {
        Particle *p = [particles objectAtIndex:i];
        if([p isAlive])
        {
            [p update:image];
            
            i++;
        } else {
            [p.image removeFromSuperview];
            [particles removeObject:p];
        }
    }
}

- (void) setBackgroundWithColor:(UIColor *)color
{
    self.view.backgroundColor = color;
}


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
    return arrayOfColors.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row   forComponent:(NSInteger)component
{
    return [[arrayOfColors objectAtIndex:(row-1)] stringValue];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row   inComponent:(NSInteger)component
{
    self.view.backgroundColor = [arrayOfColors objectAtIndex:(row - 1)];
}

@end
